<?php
if ($do === 'postAdd_dates') {
	if ($_POST['CreateNewRecord']) {
		foreach ($_POST['CreateNewRecord'] as $CreateNewRecord){
			if ($CreateNewRecord[5]) {
				$sql = "INSERT INTO `album` SET  
					`artist`='".mysql_real_escape_string($CreateNewRecord[1])."', 
					`title`='".mysql_real_escape_string($CreateNewRecord[2])."', 
					`add_date`='".mysql_real_escape_string($CreateNewRecord[0])."',
					`label`='".mysql_real_escape_string($CreateNewRecord[3])."' ";
					mysql_query($sql) or die (mysql_error());
			}
		}
	}
	if ($_POST['UpdateAddRecord']) {
			foreach ($_POST['UpdateAddRecord'] as $UpdateAddRecord){
			if ($UpdateAddRecord[5]) {
				$sql2 = "UPDATE `album` SET  
					`add_date`='".mysql_real_escape_string($UpdateAddRecord[0])."',
					`label`='".mysql_real_escape_string($UpdateAddRecord[3])."'
					WHERE `cddbid`='".mysql_real_escape_string($UpdateAddRecord[4])."'
					";
				mysql_query($sql2) or die (mysql_error());
			}
		}
	}
} elseif ($do === 'confirm') {
?>


<form action="<?=$_SERVER['PHP_SELF']?>" method="post"><?php
for ($i = 0; $i < count($ArrayToSplit = $MasterArray = explode("\n", $_POST['add_dates'])) ; $i++)
{
	$DateCheckSplit[$i] = explode(' ', $ArrayToSplit[$i]);
	if (ctype_alpha($DateCheckSplit[$i][0]) && ctype_digit(trim(rtrim($DateCheckSplit[$i][1], ','))) && ctype_digit(trim($DateCheckSplit[$i][2])) && strtotime($AddDate[$i] = ($DateCheckSplit[$i][0] . ' ' . $DateCheckSplit[$i][1] . ' ' . $DateCheckSplit[$i][2]))) {
		$LastAddDate = date('Y-m-d', strtotime($AddDate[$i]));
	}
	$AddDateArray[$i] = explode('	', $MasterArray[$i]);
	$AddDateArray[$i][0] = $LastAddDate;
}

?>

<?php
$DoNothingCount = 0;
$UpdateAddDateCount = 0;
$CreateNewCount = 0;
for ($j = 0; $j < count($AddDateArray); $j++ ){
	$Match = false;
	$record_exists = "
		SELECT * FROM `album` 
		WHERE LOWER(`artist`) LIKE '%".mysql_real_escape_string(strtolower(trim($AddDateArray[$j][1])))."%' 
		AND LOWER(`title`) LIKE '%".mysql_real_escape_string(strtolower(trim($AddDateArray[$j][2])))."%' ";
	$record_exists_query = mysql_query($record_exists);
	while($record = mysql_fetch_array($record_exists_query)) {
		if ($record[add_date] == $AddDateArray[$j][0]) {
			$DoNothing[$DoNothingCount] = $AddDateArray[$j];
			$DoNothingCount++;
		} else {
			$UpdateAddDate[$UpdateAddDateCount] = $AddDateArray[$j];
			$UpdateAddDate[$UpdateAddDateCount][4] = $record[cddbid];
			$UpdateAddDateCount++;
		}
		$Match = true;
	}
	if (!$Match) {
		$CreateNew[$CreateNewCount] = $AddDateArray[$j];
		$CreateNewCount++;
	}
}
?>
<input type="hidden" name="cat" value="<?=$cat?>" />
<input type="hidden" name="mode" value="adddateimport" />
<input type="hidden" name="do" value="postAdd_dates" />
<input type="submit" value="Add CDs" />

<h3>The following will be created as new CDs:</h3>
<table>
<tr><th></th><th>Artist</th><th>Album</th><th>Add Date</th></tr>
<?
for ($i = 0; $i < count($CreateNew); $i++) {
	echo('<tr><td><input type="checkbox" name="CreateNewRecord[' . $i . '][5]" checked="checked" value="true" /></td><td><input type="text" name="CreateNewRecord[' . $i . '][2]" value="' . htmlspecialchars(trim($CreateNew[$i][2])) . '" /></td><td><input type="text" name="CreateNewRecord[' . $i . '][1]" value="' . htmlspecialchars(trim(ucwords(strtolower($CreateNew[$i][1])))) . '" /></td><td><input type="text" name="CreateNewRecord[' . $i . '][0]" value="' . htmlspecialchars(trim($CreateNew[$i][0])) . '" /><input type="hidden" name="CreateNewRecord[' . $i . '][3]" value="' . htmlspecialchars(trim($CreateNew[$i][3])) . '" /></td></tr>');
}
?></table>
<h3>The following will be updated to reflect new values:</h3>
<table>
<tr><th></th><th>Artist</th><th>Album</th><th>Add Date</th></tr>
<?
for ($i = 0; $i < count($UpdateAddDate); $i++) {
	echo('<tr><td><input type="checkbox" name="UpdateAddRecord[' . $i . '][5]" value="true" /></td><td><input type="text" name="UpdateAddRecord[' . $i . '][2]" value="' . htmlspecialchars(trim($UpdateAddDate[$i][2])) . '" /></td><td><input type="text" name="UpdateAddRecord[' . $i . '][1]" value="' . htmlspecialchars(trim(ucwords(strtolower($UpdateAddDate[$i][1])))) . '" /></td><td><input type="text" name="UpdateAddRecord[' . $i . '][0]" value="' . htmlspecialchars(trim($UpdateAddDate[$i][0])) . '" /><input type="hidden" name="UpdateAddRecord[' . $i . '][4]" value="' . htmlspecialchars(trim($UpdateAddDate[$i][4])) . '" /><input type="hidden" name="UpdateAddRecord[' . $i . '][3]" value="' . htmlspecialchars(trim($UpdateAddDate[$i][3])) . '" /></td></tr>');
}
?></table>
<h3>Nothing will be done with the following CDs:</h3>
<table>
<tr><th></th><th>Artist</th><th>Album</th><th>Add Date</th></tr>
<?
for ($i = 0; $i < count($DoNothing); $i++) {
	echo('<tr><td></td><td>' . $DoNothing[$i][2] . '</td><td>' . $DoNothing[$i][1] . '</td><td>' . $DoNothing[$i][0] . '</td></tr>');
}
?></table>
<?php
} else {
?>
</form>
<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
<fieldset>
<legend>Copy and paste CMJ add date information below</legend>
<textarea name="add_dates" style="height: 350px; width: 600px;"></textarea>
<br />
<input type="hidden" name="mode" value="adddateimport" />
<input type="hidden" name="do" value="confirm" />
<input type="hidden" name="cat" value="<?=$cat?>" />
<input type="submit" value="Import"/><input type="reset" value="Clear" />
</fieldset>
</form>
<?php
}
?>